package com.nanhang.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanhang.common.util.PageResult;
import com.nanhang.entity.Classes;
import com.nanhang.entity.Student;
import com.nanhang.mapper.StudentMapper;
import com.nanhang.service.IClassesService;
import com.nanhang.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanhang.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
	@Autowired
	private IClassesService classesService;
	@Override
	public PageResult<Student> findByPage(Integer page, Integer pageSize) {
		Page<Student> page1 = this.page(new Page<>(page, pageSize));
		return new PageResult<>(page1.getRecords(),page1.getTotal());
	}
	@Override
	public PageResult<Student> search(Integer page, Integer pageSize, StudentVO vo) {
		QueryWrapper<Student> wrapper = new QueryWrapper<>();
		wrapper.like(StrUtil.isNotBlank(vo.getSname()), "sname",  vo.getSname() )
			   .like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr())
			   .eq(vo.getCid() != null && vo.getCid() != 0, "cid", vo.getCid());
		Page<Student> page1 = this.page(new Page<>(page, pageSize), wrapper);
		page1.getRecords().forEach(st -> {
			Classes classes = classesService.getById(st.getCid());
			st.setCname(classes.getCname());
		});
		return new PageResult<>(page1.getRecords(),page1.getTotal());
	}
	@Override
	public boolean add(Student student) {
		return this.save(student);
	}
}
